//
//  ViewController.m
//  socketDemo
//
//  Created by 吳瀾洲 on 2020/7/28.
//  Copyright © 2020 kingboyrang. All rights reserved.
//

#import "ViewController.h"
#import "TcpClient.h"
#import "UdpManager.h"
#import "NSString+CZExtend.h"
#import "QMUIKit.h"
#import "SVProgressHUD.h"
#import "LoadPopView.h"
#import "BaseDataUtils.h"

@interface ViewController ()
@property(nonatomic,strong) LoadPopView *loadPopView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.loadPopView = [LoadPopView loadXibView];
    // Do any additional setup after loading the view.
    
}



//发送内容
-(IBAction)sendBtnAction:(id)sender {
    
      if([[self.hostField.text Trim] length] == 0){
          [QMUITips showError:@"主机IP不为空"];
          return;
      }
      if([[self.portField.text Trim] length] == 0){
          [QMUITips showError:@"端口不为空"];
          return;
      }
      if([[self.txtField.text Trim] length] == 0){
          [QMUITips showError:@"发送内容不为空"];
          return;
      }
    
    //发送数据
      NSString *msgData = [self.txtField.text Trim];
      NSData *sendData = nil;
      if(self.hexSwitch.on)
      {
          sendData = [BaseDataUtils convertHexStrToData:msgData];
      }else{
          sendData = [msgData dataUsingEncoding:NSUTF8StringEncoding];
      }
     BaseDataPack *dataPack = [[BaseDataPack alloc] init];
     dataPack.packetId = 1;
     dataPack.bodyData = sendData;
     __weak __typeof(self) weakSelf = self;
    if (self.waySwitch.on) { //tcp发送
        
        
        //内容重置
        [self.loadPopView resetLabel];
        [self.loadPopView showInView:self.view];
   
        TcpClient *tcp = [TcpClient shareInstance];
        //连接状态
        [tcp getConnector].connectStateChangedBlock = ^(TcpConnectState conState){
            
            if (conState == TcpConnectSuccess) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    weakSelf.loadPopView.labConnectStatus.text = @"连接成功";
                     weakSelf.loadPopView.labSendStatus.text = @"发送数据中...";
                    [[tcp getConnector] sendDataPack:dataPack];
                });
                
            }
            if (conState == TcpConnectFailed) {
                dispatch_async(dispatch_get_main_queue(), ^{
                      weakSelf.loadPopView.labConnectStatus.text = @"连接失败";
                      weakSelf.labStatus.text = @"连接失败";
                    //2秒后关闭弹框
                    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC));
                    dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                        //关闭弹框
                        [weakSelf.loadPopView dismiss];
                    });
                });
            }
            if (conState == TcpConnectClose) {
                dispatch_async(dispatch_get_main_queue(), ^{
                  weakSelf.labStatus.text = @"连接关闭";
                  weakSelf.loadPopView.labCloseStatus.text = @"断开连接成功";
                });
                //2秒后关闭弹框
                dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC));
                dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                    //关闭弹框
                    [weakSelf.loadPopView dismiss];
                });
            }
            
        };
        //发送状态
        [tcp getConnector].sendDataStateBlock = ^(BOOL success, long tag, NSError *error){
            dispatch_async(dispatch_get_main_queue(), ^{
                weakSelf.labStatus.text = @"发送数据成功";
                weakSelf.loadPopView.labSendStatus.text = @"发送数据成功";
                weakSelf.loadPopView.labCloseStatus.text = @"断开连接中...";
            });
            //2秒后关闭连接
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC));
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                //关闭连接
                [tcp disconnect];
            });
            
        };
        [tcp connectToHost:[self.hostField.text Trim] port:[[self.portField.text Trim] integerValue]];
        
        
        
        
    }else{ //udp发送
       
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
        [SVProgressHUD showWithStatus:@"发送中..."];
        UdpManager *udp = [UdpManager shareInstance];
        udp.connectStateChangedBlock=^(UDPConnectState conState){
            
            NSLog(@"udp connect");
        };
        udp.sendDataBlock = ^(BOOL success, long tag, NSError *error) {
            
            if (success) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    weakSelf.labStatus.text = @"发送数据成功";
                               [SVProgressHUD showSuccessWithStatus:@"发送数据成功"];
                    //2秒后关闭
                    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC));
                    dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                        //关闭
                        [SVProgressHUD dismiss];
                    });
                });
                
            }else{
                dispatch_async(dispatch_get_main_queue(), ^{
                        weakSelf.labStatus.text = @"发送数据失败";
                    [SVProgressHUD showErrorWithStatus:@"发送数据失败"];
                    //2秒后关闭
                    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC));
                    dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                        //关闭
                        [SVProgressHUD dismiss];
                    });
                });
                
            }
            
           
            
        };
        [udp sendData:dataPack.bodyData port:[[self.portField.text Trim] integerValue] toHost:[self.hostField.text Trim]];
    }
    
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.txtField resignFirstResponder];
    [self.hostField resignFirstResponder];
    [self.portField resignFirstResponder];
}


@end
